package main

import (
	"fmt"
	"reflect"
)

type Student struct {
	Name string
	Age  int
}

func main() {

	var num = 100
	reflectDemo(num)

	var stu = Student{
		Name: "tom",
		Age:  1,
	}
	reflectDemo2(stu)
}

func reflectDemo(n interface{}) {
	//通过反射获取的传入的变量的 type , kind, 值
	//1. 先获取到 reflect.Type
	vType := reflect.TypeOf(n)
	fmt.Println(vType)

	//2、获取：reflect.Value
	nValue := reflect.ValueOf(n)
	n2 := nValue.Int() + 2
	fmt.Println(n2)
	fmt.Printf("nValue type =  %T,value = %v\n", nValue, nValue)

	//3、下面我们将 nValue 转成 interface{} iV := nValue.Interface()
	//将 interface{}  通过断言转成需要的类型
	intface := nValue.Interface()
	fmt.Printf("intface type %T\n", intface)
	n3 := intface.(int)
	fmt.Println(n3)
}

func reflectDemo2(n interface{}) {
	//通过反射获取的传入的变量的 type , kind, 值
	//1. 先获取到 reflect.Type
	vType := reflect.TypeOf(n)
	fmt.Println(vType)

	//2、获取：reflect.Value
	nValue := reflect.ValueOf(n)

	//3、先将value类型转为interface{}，然后将 interface{}  通过断言转成需要的类型
	intface := nValue.Interface()
	student := intface.(Student)
	fmt.Println(student)
}
